﻿using Client.Control;
using CommonUtil;
using Newtonsoft.Json.Linq;
using System;
using System.Windows;
using System.Windows.Media;

namespace Client.Windows {
    /// <summary>
    /// DragOverWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DragOverWindow : Window {
        static DragOverWindow dragOverWindow = new DragOverWindow();

        SocketUtil socket = null;

        private CircleBall cb {
            get {
                foreach (Window window in Application.Current.Windows) {
                    if ("CircleBall".Equals(window.Title)) {
                        return (CircleBall)window;
                    }
                }
                return null;
            }
        }

        private DragOverWindow() {
            InitializeComponent();

            socket = SocketUtil.GetSocket();
        }

        public static DragOverWindow GetDragOverWindow() {
            if (dragOverWindow == null) {
                dragOverWindow = new DragOverWindow();
            }
            return dragOverWindow;
        }

        private void Grid_Drop(object sender, DragEventArgs e) {
            LogUtil.logger.Debug("释放文件");
            Circle c = (Circle)sender;
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            JObject jObject = new JObject();
            JArray jArray = new JArray();
            foreach (string item in files) {
                jArray.Add(item);
            }
            jObject["type"] = (string)c.Tag;
            jObject["fileList"] = jArray;
            socket.Emit("addFiles", jObject);
            switch ((string)c.Tag) {
                case "word":
                    cb.CenterCircle.TaskType = TaskType.Word;
                    break;
                case "excel":
                    cb.CenterCircle.TaskType = TaskType.Excel;
                    break;
                case "pdf":
                    cb.CenterCircle.TaskType = TaskType.Pdf;
                    break;
            }
            Hide();
        }

        private void Grid_DragEnter(object sender, DragEventArgs e) {
            Circle x = (Circle)sender;
            x.Opacity = 0.5;
        }

        private void Grid_DragLeave(object sender, DragEventArgs e) {
            Circle x = (Circle)sender;
            x.Opacity = 1;
        }

        private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) {
            if ((bool)e.NewValue) {

                double x = cb.Left + cb.Width + 10;
                double y = cb.Top + (cb.Height - 60) / 2;
                if (x + Width > SystemParameters.WorkArea.Right) {
                    x = cb.Left - cb.Width - 10;
                }
                if (y + Height > SystemParameters.WorkArea.Bottom) {
                    y = cb.Top - Height + cb.Height - (cb.Height - 60) / 2;
                }
                dragOverWindow.Left = x;
                dragOverWindow.Top = y;
            } else {
                WordCircle.Opacity = 1;
                ExcelCircle.Opacity = 1;
                PdfCircle.Opacity = 1;
            }
        }
    }
}
